package com.gs.disk.modules.headImgAndWallper.dao.admin;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gs.common.mapper.CommonMapper;
import com.gs.disk.modules.headImgAndWallper.entity.CategoryEntity;
import com.gs.disk.modules.headImgAndWallper.entity.ResourceEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.jdbc.SQL;

import java.util.List;

@Mapper
public interface AdminResourceDao extends CommonMapper<ResourceEntity, ResourceEntity> {


    class AdminResourceProvider {

        public String list(Long categoryId) {
            return new SQL(){{
                SELECT("e.*");
                FROM("wallper_resource e, wallper_category c");
                WHERE("(c.id = e.category_id or c.pid = e.category_id)");

                if(ObjectUtil.isNotEmpty(categoryId)) {
                    AND();
                    WHERE("c.id = #{categoryId} or c.pid = #{categoryId}");
                }
            }}.toString();
        }
    }
    @SelectProvider(type = AdminResourceProvider.class, method = "list")
    List<ResourceEntity> list(Long categoryId);

    /**
     * 所有资源中的最大排序
     * @return
     */
    default Integer getMaxSort() {

        Integer currentSort = get("select max(sort) max_sort from wallper_resource", Integer.class);

        if(ObjectUtil.isNull(currentSort)) return null;

        return currentSort + 1;
    }
}
